import { Module, Global, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { YuicService } from './yuic.service';
import { YuicService2 } from './yuic.service2';
import { YuicController } from './yuic.controller';
import { APP_GUARD } from '@nestjs/core';
import { FunctionMiddleware } from '../middlerware/function.middleware';
// import { RolesGuard } from '../guard/roles.guard';
function sleep() {
  return new Promise((resolve, rej) => {
    setTimeout(() => {
      resolve('111');
    }, 1000);
  });
}
@Global()
@Module({
  controllers: [YuicController],
  providers: [
    YuicService,
    {
      provide: 'yuiyi',
      useClass: YuicService,
    },
    {
      provide: 'yuiyivalue',
      useValue: ['a', 'b', 'c'],
    },
    YuicService2,
    {
      provide: 'FactoryProvider',
      //返回要注入的提供程序的实例
      useFactory: (YuicService2: YuicService2) => {
        const name = YuicService2.findAll();
        return name;
      },
      inject: [YuicService2],
    },
    {
      provide: 'FactoryProviderPromise',
      //返回要注入的提供程序的实例
      useFactory: async (YuicService2: YuicService2) => {
        const name = YuicService2.findAll();
        return await sleep();
        //  name;
      },
      inject: [YuicService2],
    },
    // {
    //   provide: APP_GUARD,
    //   useClass: RolesGuard,
    // },
  ],
  exports: [YuicService],
})
export class YuicModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(FunctionMiddleware).forRoutes('yui');
  }
}
// export class YuicModule implements NestModule {
//   configure(consumer: MiddlewareConsumer) {
//     consumer.apply(FunctionMiddleware).forRoutes('yui');
//   }
// }
